﻿using Game.Entity.Accounts;
using Game.Facade;
using Game.Kernel;
using Game.Utils;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;

namespace Game.Card
{
    /// <summary>
    /// Handler 的摘要说明
    /// </summary>
    public class Handler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "application/json";
            string action = GameRequest.GetQueryString("action").ToLower();
            switch(action)
            {
                case "getnicknamebygameid":
                    GetNickNameByGameID(context);
                    break;
                case "gettotalroomcardrebate":
                    GetTotalRoomCardRebate(context);
                    break;
                case "gettodayroomcardrebate":
                    GetTodayRoomCardRebate(context);
                    break;
                case "getroomcardbuylist":
                    GetRoomCardBuyList(context);
                    break;
                case "getroomcardrebatelist":
                    GetRoomCardRebateList(context);
                    break;
                case "getroomcardexchlist":
                    GetRoomCardExchList(context);
                    break;
                case "getroomcardcostlist":
                    GetRoomCardCostList(context);
                    break;
                case "getroomcardbalance":
                    GetRoomCardBalance(context);
                    break;
                case "getroomcardpresentlist":
                    GetRoomCardPresentList(context);
                    break;
                default:
                    break;

            }
        }

        /// <summary>
        /// 获取用户昵称
        /// </summary>
        /// <param name="context"></param>
        protected void GetNickNameByGameID(HttpContext context)
        {
            AjaxJsonValid ajv = new AjaxJsonValid();

            int gameid = GameRequest.GetFormInt("gameid", 0);

            string account = FacadeManage.aideAccountsFacade.GetAccountsByGameID(gameid);
            ajv.AddDataItem("account", account);
            ajv.SetValidDataValue(true);
            context.Response.Write(ajv.SerializeToJson());
        }
        /// <summary>
        /// 获取房卡总返利
        /// </summary>
        /// <param name="context"></param>
        protected void GetTotalRoomCardRebate(HttpContext context)
        {
            AjaxJsonValid ajv = new AjaxJsonValid();

            int userid = GameRequest.GetFormInt("userid", 0);

            string rebate = FacadeManage.aidePlatformFacade.GetTotalRoomCardRebate(userid);
            ajv.AddDataItem("rebate", rebate);
            ajv.SetValidDataValue(true);
            context.Response.Write(ajv.SerializeToJson());
        }
        /// <summary>
        /// 获取今日房卡返利
        /// </summary>
        /// <param name="context"></param>
        protected void GetTodayRoomCardRebate(HttpContext context)
        {
            AjaxJsonValid ajv = new AjaxJsonValid();

            int userid = GameRequest.GetFormInt("userid", 0);

            string rebate = FacadeManage.aidePlatformFacade.GetTodayRoomCardRebate(userid);
            ajv.AddDataItem("rebate", rebate);
            ajv.SetValidDataValue(true);
            context.Response.Write(ajv.SerializeToJson());
        }
        /// <summary>
        /// 获取房卡购买记录
        /// </summary>
        /// <param name="context"></param>
        protected void GetRoomCardBuyList(HttpContext context)
        {
            AjaxJsonValid ajv = new AjaxJsonValid();

            //判断登录
            UserTicketInfo uti = Fetch.GetUserCookie();
            if(uti == null || uti.UserID <= 0)
            {
                ajv.code = 0;
                ajv.msg = "登录已失效，请刷新页面重新登陆";
                context.Response.Write(ajv.SerializeToJson());
                return;
            }

            StringBuilder sb = new StringBuilder();
            int number = GameRequest.GetQueryInt("pageSize", 10);
            int page = GameRequest.GetQueryInt("page", 1);

            string where = " WHERE TypeID = 0 AND SourceUserID = " + uti.UserID;
            PagerSet pagerSet = FacadeManage.aideRecordFacade.GetRoomCardRecord(where, page, number);
            string html = string.Empty;
            if(pagerSet.PageSet.Tables[0].Rows.Count > 0)
            {
                foreach(DataRow item in pagerSet.PageSet.Tables[0].Rows)
                {
                    sb.Append("<tr>");
                    sb.AppendFormat("<td>{0}</td>", (Convert.ToDateTime(item["CollectDate"]).ToString("yyyy-MM-dd")+"<br/>" + Convert.ToDateTime(item["CollectDate"]).ToString("HH:mm:ss")));
                    sb.AppendFormat("<td>{0}房卡</td>", item["RoomCard"]);
                    sb.AppendFormat("<td>{0}游戏豆</td>", Convert.ToInt32(item["Currency"]));
                    sb.AppendFormat("<td>{0}房卡</td>", (Convert.ToInt32(item["SBeforeCard"])+ Convert.ToInt32(item["RoomCard"])));
                    sb.Append("</tr>");
                }
                html = sb.ToString();
            }
            else
            {
                html = "<tr><td colspan=\"4\">暂无记录！</td></tr>";
            }
            ajv.AddDataItem("html", html);
            ajv.AddDataItem("total", pagerSet.RecordCount);
            ajv.SetValidDataValue(true);
            context.Response.Write(ajv.SerializeToJson());
        }
        /// <summary>
        /// 获取房卡返利记录
        /// </summary>
        /// <param name="context"></param>
        protected void GetRoomCardRebateList(HttpContext context)
        {
            AjaxJsonValid ajv = new AjaxJsonValid();

            //判断登录
            UserTicketInfo uti = Fetch.GetUserCookie();
            if(uti == null || uti.UserID <= 0)
            {
                ajv.code = 0;
                ajv.msg = "登录已失效，请刷新页面重新登陆";
                context.Response.Write(ajv.SerializeToJson());
                return;
            }

            StringBuilder sb = new StringBuilder();
            int number = GameRequest.GetQueryInt("pageSize", 10);
            int page = GameRequest.GetQueryInt("page", 1);

            string where = " WHERE UserID = " + uti.UserID + " AND DissumeDate IS NOT NULL";
            PagerSet pagerSet = FacadeManage.aidePlatformFacade.GetRoomCardStream(where, page, number);
            string html = string.Empty;
            if(pagerSet.PageSet.Tables[0].Rows.Count > 0)
            {
                foreach(DataRow item in pagerSet.PageSet.Tables[0].Rows)
                {
                    sb.Append("<tr>");
                    sb.AppendFormat("<td>{0}</td>", item["RoomID"]);
                    sb.AppendFormat("<td>{0}房卡</td>", item["CreateTableFee"]);
                    sb.AppendFormat("<td>{0}游戏币</td>", Convert.ToInt32(item["TaxRevenue"]));
                    sb.AppendFormat("<td>{0}</td>", (Convert.ToDateTime(item["DissumeDate"]).ToString("yyyy-MM-dd") + "<br/>" + Convert.ToDateTime(item["DissumeDate"]).ToString("HH:mm:ss")));
                    sb.Append("</tr>");
                }
                html = sb.ToString();
            }
            else
            {
                html = "<tr><td colspan=\"4\">暂无记录！</td></tr>";
            }
            ajv.AddDataItem("html", html);
            ajv.AddDataItem("total", pagerSet.RecordCount);
            ajv.SetValidDataValue(true);
            context.Response.Write(ajv.SerializeToJson());
        }
        /// <summary>
        /// 获取房卡兑换记录
        /// </summary>
        /// <param name="context"></param>
        protected void GetRoomCardExchList(HttpContext context)
        {
            AjaxJsonValid ajv = new AjaxJsonValid();

            //判断登录
            UserTicketInfo uti = Fetch.GetUserCookie();
            if(uti == null || uti.UserID <= 0)
            {
                ajv.code = 0;
                ajv.msg = "登录已失效，请刷新页面重新登陆";
                context.Response.Write(ajv.SerializeToJson());
                return;
            }

            StringBuilder sb = new StringBuilder();
            int number = GameRequest.GetQueryInt("pageSize", 10);
            int page = GameRequest.GetQueryInt("page", 1);

            string where = " WHERE TypeID = 2 AND SourceUserID = " + uti.UserID;
            PagerSet pagerSet = FacadeManage.aideRecordFacade.GetRoomCardRecord(where, page, number);
            string html = string.Empty;
            if(pagerSet.PageSet.Tables[0].Rows.Count > 0)
            {
                foreach(DataRow item in pagerSet.PageSet.Tables[0].Rows)
                {
                    sb.Append("<tr>");
                    sb.AppendFormat("<td>{0}</td>", (Convert.ToDateTime(item["CollectDate"]).ToString("yyyy-MM-dd") + "<br/>" + Convert.ToDateTime(item["CollectDate"]).ToString("HH:mm:ss")));
                    sb.AppendFormat("<td>{0}游戏币</td>", item["Gold"]);
                    sb.AppendFormat("<td>{0}房卡</td>", item["RoomCard"]);
                    sb.AppendFormat("<td>{0}房卡</td>", (Convert.ToInt32(item["SBeforeCard"]) - Convert.ToInt32(item["RoomCard"])));
                    sb.Append("</tr>");
                }
                html = sb.ToString();
            }
            else
            {
                html = "<tr><td colspan=\"4\">暂无记录！</td></tr>";
            }
            ajv.AddDataItem("html", html);
            ajv.AddDataItem("total", pagerSet.RecordCount);
            ajv.SetValidDataValue(true);
            context.Response.Write(ajv.SerializeToJson());
        }
        /// <summary>
        /// 获取房卡消耗记录
        /// </summary>
        /// <param name="context"></param>
        protected void GetRoomCardCostList(HttpContext context)
        {
            AjaxJsonValid ajv = new AjaxJsonValid();

            //判断登录
            UserTicketInfo uti = Fetch.GetUserCookie();
            if(uti == null || uti.UserID <= 0)
            {
                ajv.code = 0;
                ajv.msg = "登录已失效，请刷新页面重新登陆";
                context.Response.Write(ajv.SerializeToJson());
                return;
            }

            StringBuilder sb = new StringBuilder();
            int number = GameRequest.GetQueryInt("pageSize", 10);
            int page = GameRequest.GetQueryInt("page", 1);

            string where = " WHERE CardOrBean = 1 AND UserID = " + uti.UserID;
            PagerSet pagerSet = FacadeManage.aidePlatformFacade.GetRoomCardStream(where, page, number);
            string html = string.Empty;
            if(pagerSet.PageSet.Tables[0].Rows.Count > 0)
            {
                foreach(DataRow item in pagerSet.PageSet.Tables[0].Rows)
                {
                    sb.Append("<tr>");
                    sb.AppendFormat("<td>{0}</td>", (Convert.ToDateTime(item["CreateDate"]).ToString("yyyy-MM-dd") + "<br/>" + Convert.ToDateTime(item["CreateDate"]).ToString("HH:mm:ss")));
                    sb.AppendFormat("<td>{0}房卡</td>", item["CreateTableFee"]);
                    sb.AppendFormat("<td>{0}</td>", item["RoomID"]);
                    sb.AppendFormat("<td><a href=\"javascript:;\" onclick=\"lookscore('{0}')\">查看</a></td>",item["RoomID"]);
                    sb.Append("</tr>");
                }
                html = sb.ToString();
            }
            else
            {
                html = "<tr><td colspan=\"4\">暂无记录！</td></tr>";
            }
            ajv.AddDataItem("html", html);
            ajv.AddDataItem("total", pagerSet.RecordCount);
            ajv.SetValidDataValue(true);
            context.Response.Write(ajv.SerializeToJson());
        }

        /// <summary>
        /// 获取房卡房间结算
        /// </summary>
        /// <param name="context"></param>
        protected void GetRoomCardBalance(HttpContext context)
        {
            AjaxJsonValid ajv = new AjaxJsonValid();

            //判断登录
            UserTicketInfo uti = Fetch.GetUserCookie();
            if(uti == null || uti.UserID <= 0)
            {
                ajv.code = 0;
                ajv.msg = "登录已失效，请刷新页面重新登陆";
                context.Response.Write(ajv.SerializeToJson());
                return;
            }

            StringBuilder sb = new StringBuilder();
            string roomid = GameRequest.GetFormString("roomid");
            string html = string.Empty;

            DataSet ds = FacadeManage.aidePlatformFacade.GetPersonalRoomScore(roomid);
            if(ds.Tables[0].Rows.Count > 0)
            {
                foreach(DataRow item in ds.Tables[0].Rows)
                {
                    sb.AppendFormat("{0}：{1}<br/>", FacadeManage.aideAccountsFacade.GetAccountsByUserID(Convert.ToInt32(item["UserID"])), item["Score"]);
                }
                html = sb.ToString();
            }
            else
            {
                html = "该房间游戏还在进行中，请稍后查看战绩";
            }

            ajv.AddDataItem("html", html);
            ajv.SetValidDataValue(true);
            context.Response.Write(ajv.SerializeToJson());
        }

        /// <summary>
        /// 获取房卡赠送记录
        /// </summary>
        /// <param name="context"></param>
        protected void GetRoomCardPresentList(HttpContext context)
        {
            AjaxJsonValid ajv = new AjaxJsonValid();

            //判断登录
            UserTicketInfo uti = Fetch.GetUserCookie();
            if(uti == null || uti.UserID <= 0)
            {
                ajv.code = 0;
                ajv.msg = "登录已失效，请刷新页面重新登陆";
                context.Response.Write(ajv.SerializeToJson());
                return;
            }

            StringBuilder sb = new StringBuilder();
            int number = GameRequest.GetQueryInt("pageSize", 10);
            int page = GameRequest.GetQueryInt("page", 1);

            string where = " WHERE TypeID = 1 AND SourceUserID = " + uti.UserID;
            PagerSet pagerSet = FacadeManage.aideRecordFacade.GetRoomCardRecord(where, page, number);
            string html = string.Empty;
            if(pagerSet.PageSet.Tables[0].Rows.Count > 0)
            {
                foreach(DataRow item in pagerSet.PageSet.Tables[0].Rows)
                {
                    sb.Append("<tr>");
                    sb.AppendFormat("<td>{0}</td>", (Convert.ToDateTime(item["CollectDate"]).ToString("yyyy-MM-dd") + "<br/>" + Convert.ToDateTime(item["CollectDate"]).ToString("HH:mm:ss")));
                    sb.AppendFormat("<td>{0}</td>", FacadeManage.aideAccountsFacade.GetGameIDByUserID(Convert.ToInt32(item["TargetUserID"])));
                    sb.AppendFormat("<td>{0}房卡</td>", item["SBeforeCard"]);
                    sb.AppendFormat("<td>{0}房卡</td>", item["RoomCard"]);
                    sb.Append("</tr>");
                }
                html = sb.ToString();
            }
            else
            {
                html = "<tr><td colspan=\"4\">暂无记录！</td></tr>";
            }
            ajv.AddDataItem("html", html);
            ajv.AddDataItem("total", pagerSet.RecordCount);
            ajv.SetValidDataValue(true);
            context.Response.Write(ajv.SerializeToJson());
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}